var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    SearchValue: "",
    choose_index: 1,
    order_type: 0,
    search_type: 0,
    cate_id:"",
    sort: {
      up: 0,
      down: 0
    },

    current_page: 1,
    last_page: 1,
  },
  getMoreData() {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let page = Number(this.data.current_page) + 1;
    let json = {
      page,
      order_type: this.data.order_type,
      search_type: this.data.search_type
    }
    if(this.data.cate_id){
      json.cate_id = this.data.cate_id;
    }
    if(this.data.SearchValue){
      json.title=this.data.SearchValue;
    }
    this.getData(json)
  },
  onSearch({
    detail
  }) {
    this.setData({
      SearchValue: detail
    })
    this.getData({
      page: 1,
      title: detail,
      search_type: this.data.search_type,
      order_type: this.data.order_type
    })
  },
  choose(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      choose_index: index
    })

    if (index !== 1) {
      if (this.data.sort.up == 0 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      } else if (this.data.sort.up == 1 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 0,
          "sort.down": 1
        })
      } else if (this.data.sort.up == 0 && this.data.sort.down == 1) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      }
    } else if (index == 1) {
      this.setData({
        "sort.up": 0,
        "sort.down": 0
      })
    }
    if (this.data.sort.down == 1) {
      this.setData({
        order_type: 1
      })
    } else {
      this.setData({
        order_type: 0
      })

    }
    let search_type = index - 1;
    this.setData({
      search_type
    })
    this.getData({
      page: 1,
      order_type: this.data.order_type,
      search_type,
      title: this.data.SearchValue
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let json = {
      page: 1,
      title: this.data.SearchValue,
      search_type: this.data.search_type,
      order_type: this.data.order_type
    }
    if (options.cate_id) {
      json.cate_id = options.cate_id;
      this.setData({cate_id:options.cate_id})
    } 
    if (options.title) {
      json.title = options.title;
      this.setData({SearchValue:options.title})
    } 

    this.getData(json)
  },

  getData(json) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.tiktok-cps.api.goods.good-list"),
      data: json,
      success: resdata => {
        let data = resdata.data;
        if (data.result == 0) {
          return app.tips(data.msg)
        }
        if (json.page == 1) {
          this.setData({
            list: data.data.products
          })
        } else if (json.page > 1) {
          let list = this.data.list.concat(data.data.products)
          this.setData({
            list
          })
        }
        this.setData({
          current_page: data.data.current_page,
          last_page: data.data.last_page,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})